<%--
  Created by IntelliJ IDEA.
  User: ttxs
  Date: 2023/11/13
  Time: 15:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fm" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/statics/js/jquery-1.8.3.min.js"></script>
    <script>
        $(function () {
            let exists = true;
            $("[name=stuno]").change(function () {
                let stuno = $(this).val();
                if (stuno) {
                    $.getJSON('${pageContext.request.contextPath}/stu/exists',{stuno:stuno},function (result) {
                        exists = result;
                        alert(exists ? "已经存在" : "可以使用");
                    })
                }
            })
            $.getJSON('${pageContext.request.contextPath}/stuClass/list',function (result) {
                for(let item of result) {
                    if ('${stu.classId}' == item.id) {
                        $("[name=classId]").append("<option selected value='"+item.id+"'>"+item.cname+"</option>")
                    } else {
                        $("[name=classId]").append("<option value='"+item.id+"'>"+item.cname+"</option>")
                    }
                }
            })
            $("form").submit(function () {
                let reg = /^1\d{10}$/;
                if (reg.test($("[name=phone]").val()) == false) {
                    alert("手机号码格式不正确");
                    return false;
                }
                if ($("input[type=checkbox]:checked").length == 0) {
                    alert("至少选中一个技能");
                    return false;
                }
                if (exists) {
                    alert("学号已经存在");
                    return false;
                }
                return true;
            });
        })
    </script>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/stu/add">
    <table>
        <tr>
            <td>头像</td>
            <td>
                <input required type="file" name="uploadFile">
            </td>
        </tr>
        <tr>
            <td>学号</td>
            <td>
                <input required name="stuno">
            </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input required name="realname"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td>

                <input checked type="radio" name="sex" value="0">男
                <input type="radio" name="sex" value="1">女
            </td>
        </tr>
        <tr>
            <td>介绍</td>
            <td><textarea rows="2" name="description"></textarea></td>
        </tr>
        <tr>
            <td>classId</td>
            <td>
                <select required name="classId"><option value="">请选择</option></select>
            </td>
        </tr>
        <tr>
            <td>入学日期</td>
            <td><input required type="date" name="admissionDate" value="<fmt:formatDate value="${stu.admissionDate}" pattern="yyyy-MM-dd"/>"></td>
        </tr>
        <tr>
            <td>电话</td>
            <td><input required name="phone"></td>
        </tr>
        <tr>
            <td>技能</td>
            <td>

                <input name="skills[0].sname" type="checkbox" value="java">java
                <input name="skills[1].sname" type="checkbox" value="c#">c#
                <input name="skills[2].sname" type="checkbox" value="php">php
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="添加">
                <a href="javascript:void(0)" onclick="location.href='${pageContext.request.contextPath}/stu/backToList'">返回</a>
            </td>
        </tr>
    </table>
</form>

</body>
</html>
